<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" />
        <!-- 树形组件 -->
        <el-tree
          ref="deptTree"
          :data="depts"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          node-key="id"
          @current-change="selectNode"
        />
        <!--  highlight-current 等同于 :highlight-current="true" -->
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary">添加员工</el-button>
          <el-button size="mini">excel导入</el-button>
          <el-button size="mini">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table v-loading="loading" :data="list">
          <el-table-column label="头像" width="80px">
            <!-- v-slot="{ row }" -->
            <!-- v-slot:default="{ row }" -->
            <!-- #default="{ row }" -->
            <!-- slot-scope="{ row }" V2过时的一种写法 -->
            <!-- !!把类型转化成布尔值 -->
            <template #default="{ row }">
              <img v-if="!!row.staffPhoto" class="avatar" :src="row.staffPhoto" alt="">
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="手机号" sortable prop="mobile" />
          <el-table-column label="工号" sortable prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <!--  -->
            <template v-slot:default="{ row }">
              <span v-if="row.formOfEmployment === 1">正式工</span>
              <span v-else-if="row.formOfEmployment === 2">外包</span>
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="操作">
            <template>
<<<<<<< HEAD
              <el-button type="text" size="mini">详情</el-button>
=======
              <el-button type="text" size="mini">查看详情</el-button>
>>>>>>> dev
              <el-button type="text" size="mini">角色</el-button>
              <el-button type="text" size="mini">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px;" align="middle" type="flex" justify="end">
          <el-pagination
            layout="total,prev,pager,next"
            :total="total"
            :current-page="queryParams.page"
            :page-size="queryParams.pagesize"
            @current-change="changePage"
          />
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import { getDepartMentAPI, getEmployeeListAPI } from '@/api'
import { transListToTreeData } from '@/utils'

export default {
  name: 'Employee',
  data() {
    return {
      depts: [], // 树形数据
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      queryParams: {
        page: 1,
        pagesize: 10,
        departmentId: null
      },
      total: 0, // 总数
      list: [], // 员工列表数据
      loading: false
    }
  },
  created() {
    this.getDepartMent()
  },
  methods: {
    async getDepartMent() {
      this.depts = transListToTreeData(await getDepartMentAPI(), 0) // 数据驱动视图
      this.queryParams.departmentId = this.depts[0].id // 记录第一个节点的id
      this.getEmployList() // 获取员工列表数据
      // 渲染是异步的！！！！
      this.$nextTick(() => {
        // 会等到上一次渲染完成
        this.$refs.deptTree.setCurrentNode(this.depts[0]) // 自动选中首个节点
      })
    },
    // 点击节点的时候记录点击的id
    selectNode(data) {
      this.queryParams.departmentId = data.id // 因为后续要根据id查询对应的数据
      this.queryParams.page = 1 // 切换了组织 要重新请求第一页数据
      this.getEmployList() // 重新加载数据
    },
    // 加载员工列表方法
    async getEmployList() {
      this.loading = true
      const { rows, total } = await getEmployeeListAPI(this.queryParams)
      this.list = rows
      this.loading = false
      this.total = total
    },
    // 切换页码
    changePage(newPage) {
      this.queryParams.page = newPage // 赋值新页码
      this.getEmployList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
    .avatar {
      height: 30px;
      width: 30px;
      border-radius: 50%;
    }
  }
}

</style>
